// pages/playlist/playlist.js
const MAX_LIMIT = 9
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperImgUrls: [
      // {
      //   url: 'http://p1.music.126.net/oeH9rlBAj3UNkhOmfog8Hw==/109951164169407335.jpg',
      // },
      // {
      //   url: 'http://p1.music.126.net/xhWAaHI-SIYP8ZMzL9NOqg==/109951164167032995.jpg',
      // },
      // {
      //   url: 'http://p1.music.126.net/Yo-FjrJTQ9clkDkuUCTtUg==/109951164169441928.jpg',
      // },
      // {
      //   url: 'http://p1.music.126.net/r-jwO8JPaFUXQ5OCAC8syA==/109951164828745065.jpg',
      // }
    ],
    musiclist : [],
    val: '',
    playingId: -1,
    playlist: [],
    // 播放栏处理
    music: {},
    playing: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSwiper()
    this._getPlaylist()
  },

  getSwiper(){
    wx.request({
      url: 'http://122.51.245.42:8000/swiper_info',
      success:((res)=>{
        console.log(res)
        let swiper_list = []
        let swiper_data = res.data.swiper 
        for(let i=0,len=swiper_data.length; i<len;i++){
          swiper_list.push(swiper_data[i].url)
          // swiper_list.add(swiper_data[i])
        }
        console.log(swiper_list)
        // console.log(res.data)
        // let swiper_urls = res.data.swiper
        this.setData({
          swiperImgUrls: swiper_list
        })
        // console.log(this.data.swiperImgUrls)
      })
    })
  },


  // _setMusiclist: function () {
  //   wx.setStorageSync('musiclist', this.data.musiclist);
  // },
  
  onSearch(event){
    wx.navigateTo({
      url: '../search/search',
    })
  //   // console.log(event.detail.keyword)
  //   let keyword = event.detail.value
  //   if(keyword.length != 0){
  //     wx.request({
  //       url: 'http://127.0.0.1:8000/search/' + keyword,
  //       data: {
  //         keyword
  //       },
  //       // method: 'POST',
  //       success: ((res) => {
  //         console.log(res.data.result.songs)
  //         this.setData({
  //           musiclist: res.data.result.songs,
  //           isSwiper: false,
  //         })
  //         this._setMusiclist()
          
  //       }),
  //       fail: ((err) => {
  //         console.log(err)
  //       })
  //     })
  //   }else{
  //     wx.showModal({
  //       title: '输入内容不能为空',
  //       content: '',
  //     })
  //   }
    
  },

  onSelect(event){
    console.log(event.currentTarget.dataset.musicid)
    const ds = event.currentTarget.dataset
    const musicid = ds.musicid
    this.setData({
      playingId: musicid
    })
    wx.navigateTo({
      url: `../player/player?musicId=${musicid}&index=${ds.index}`,
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 处理播放栏
    // WxNotificationCenter.addNotification("music", (res) => {
    //   this.setData({
    //     music: res.curPlaying,
    //     playing: res.playing,
    //     isShow: res.list_song.length
    //   });
    // }, this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      playlist: [],
    })
    this._getPlaylist();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this._getPlaylist();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  _getPlaylist() {
    wx.showLoading({
      title: '加载中',
    })
    let playlistLength = this.data.playlist.length
    let count = playlistLength / MAX_LIMIT
    if (parseInt(count) == count) {
      wx.request({
        url: 'http://122.51.245.42:8000/song_list/',
        data: {
          playcount : count + 1,
          count_limit : MAX_LIMIT
        },
        success: ((res) => {
          console.log(res)
          this.setData({
            playlist: this.data.playlist.concat(res.data.song_list),
          })
        })
      })
    }
    wx.hideLoading()
  }
})
